import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;

public class GUI extends Frame {
	Image img;
	int x;
	int y;

	public GUI(){
		super("Image Frame");
		this.x = 0;
		this.y = 0;
		this.addKeyListener(new MyKeyListener());
        MyMouseListener mouse = new MyMouseListener();
        this.addMouseMotionListener(mouse);
        this.addMouseListener(mouse);
		MediaTracker mt = new MediaTracker(this);
		img = Toolkit.getDefaultToolkit().getImage("data/map.jpg");
		mt.addImage(img,0);
		setSize(300,300);
		setVisible(true);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				dispose();
			}
		});
	}
	public void update(Graphics g){
		paint(g);
	}

	public void paint(Graphics g){
		if(img != null)
			g.drawImage(img, x, y, this);
		else
			g.clearRect(0, 0, getSize().width, getSize().height);
	}

	public void screen_up(int px) {
		y += px;
		if (y > 0)
			y = 0;
		repaint();
	}

	public void screen_down(int px) {
		y -= px;
		repaint();
	}

	public void screen_left(int px) {
		x += px;
		if (x > 0)
			x = 0;
		repaint();
	}

	public void screen_right(int px) {
		x -= px;
		repaint();
	}

	public void move_screen(int dx, int dy) {
		x += dx;
		if (x > 0)
			x = 0;
		y += dy;
		if (y > 0)
			y = 0;
		repaint();
	}

	public class MyKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			switch (ke.getKeyCode()) {
				case KeyEvent.VK_UP:
					screen_up(10);
					break;
				case KeyEvent.VK_DOWN:
					screen_down(10);
					break;
				case KeyEvent.VK_LEFT:
					screen_left(10);
					break;
				case KeyEvent.VK_RIGHT:
					screen_right(10);
					break;
			}
		}
	}

    public class MyMouseListener extends MouseInputAdapter
    {
        Point oldPos;

        public void mouseClicked(MouseEvent e)
        {
            System.out.printf("Mouse is clicked, position: %d:%d\n", e.getX(),
            e.getY());
            
        }

        public void mousePressed(MouseEvent e)
        {
            System.out.printf("Mouse is pressed, point %d:%d saved\n", e.getX(),
            e.getY());
            oldPos = e.getPoint();
        }

        public void mouseDragged(MouseEvent e)
        {
            System.out.printf("Mouse Dragged, position: %d:%d\n", e.getX(),
                e.getY());
            move_screen((int) (e.getX() - oldPos.getX()) , (int) (
                e.getY() - oldPos.getY()));
            oldPos = e.getPoint();

        }
    }

}
